package com.yuantu.judiciary.dao;

import com.yuantu.judiciary.model.domain.UserRelationDO;
import com.yuantu.judiciary.model.enums.UserRelationType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 用户关系
 *
 * @author syw
 * @since 2023-07-25
 **/
public interface IUserRelationDao extends JpaRepository<UserRelationDO, Long> {

    UserRelationDO findByEntityIdAndCreatorIdAndTypeAndDeletedFalse(Long entityId, Long creatorId, UserRelationType type);

    Page<UserRelationDO> findByCreatorIdAndTypeAndDeletedFalse(Long creatorId, UserRelationType type, Pageable pageable);

    List<UserRelationDO> findByCreatorIdAndTypeAndDeletedFalse(Long creatorId, UserRelationType type);

    @Query("select u from UserRelationDO u " +
            "left join QueryEntityDO q on u.entityId = q.id " +
            "where u.creatorId = ?1 and u.type = ?2 and u.deleted = false " +
            "and (?3 is null or ?3 = '' or q.name like concat('%', ?3, '%')) ")
    Page<UserRelationDO> getRelationByCreatorIdAndTypeAndNameLike(Long creatorId, UserRelationType type, String name, Pageable pageable);

    List<UserRelationDO> findByCreatorIdAndDeletedFalse(Long creatorId);

}
